import sys
import matplotlib.pyplot as plt
import os

from process.feature_process import *
from process.data_preprocess import *
from process.read_excel import read_data
from utils.evaluation import run_single_evaluation
from utils.visualization import plot_predictions, plot_model_comparison
from config import Config

config = Config()
wd = os.path.dirname(os.path.abspath(__file__))
excel_file = os.path.join(wd, config.config['data_path'])


if __name__ == '__main__':
    data = read_data(excel_file, is_new_format=True)
    df = del_iqr.process(data)
    print(df)
    if df is None:
        sys.exit()
    
    methods = {
        'ymsplit': ymsplit.prepare_features,
        'continus_month': continuous_month.prepare_features,
        'seasonal': seasonal.prepare_features,
        'sliding_window': sliding_window.prepare_features,
        'combined': combined.prepare_features,
        'seasonal_plus': seasonal_plus.prepare_features
    }
        
    # 运行所有方法
    all_results = {}
    for name, feature in methods.items():
        score, y_test, predictions, test_dates, y_test_full = run_single_evaluation(df, feature, 114514)
        all_results[name] = score
        
        print(f'\n{name}方法:')
        print(f'R2得分: {score:.4f}')
        print("实际值 -- 预测值 -- 偏差")
        
        # 确保使用排序后的数据，实际值预测值一一对应
        for actual, pred in zip(y_test, predictions):
            print(f"{actual:.2f} -- {pred:.2f} -- {(pred/actual - 1)}")
        
        plot_predictions(test_dates, y_test, predictions, f'{name}预测结果')
    
    # 对比图
    plot_model_comparison(all_results) 
    plt.show()